const base = 'https://yp.zjbenyu.com/api/';
export const publicFunction = {
	hrefURL: base,
	URLimg: 'https://fx.senge.com/',
	uploadimage: `${base}api/tea/uploading/uploadImage`, // 上传图片

	// 登录注册
	login: `${base}user/login`, // 登录
	logout: `${base}user/logout`, // 退出登录
	menuLists: `${base}menu/lists`, // 获取菜单
	ad_contentLists: `${base}ad_content/lists`, // 获取广告
	getGoodsListByColumnId: `${base}goods/getGoodsListByColumnId`, // 栏目商品列表
	getConfig: `${base}index/config`, // 获取通用配置


	testphone: new RegExp(/^1(3|4|5|6|7|8|9)[0-9]{9}$/), //手机号
	testcard: new RegExp(/^(\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/), //身份证
	testnum: new RegExp(/^[0-9]+.?[0-9]*/), //数字
	limit: 10,
	
	strToParams(str) {
		var newparams = {}
		for (let item of str.split('&')) {
			newparams[item.split('=')[0]] = item.split('=')[1]
		}
		return newparams
	},
	getTime10: function() { //获取当前时间戳（到秒）
		var timestamp = Date.parse(new Date());
		return timestamp / 1000;
	},

	getTime: function(time) { //时间戳转时间
		var d = new Date(parseInt(time) * 1000); //根据时间戳生成的时间对象
		var zhdate = (d.getFullYear()) + "-" +
			(d.getMonth() + 1) + "-" +
			(d.getDate())
		// (d.getMonth()+1 < 10 ? '0'+(d.getMonth()+1) : d.getMonth()+1) + "-" +
		// (d.getDate() < 10? '0'+d.getDate() : d.getDate())
		//  + " " + (d.getHours()) + ":" + 
		// (d.getMinutes()) + ":" + 
		// (d.getSeconds()); 
		return zhdate;
	},
	funpreviewImage: function(img) { // 预览图片
		uni.previewImage({
			// urls: [publicFunction.URLimg + img],
			urls: [ img],
			loop: true,
		});
	},
	toast: function(titles) {
		if (!typeof plus) {
			plus.nativeUI.toast(titles, {
				verticalAlign: 'center'
			})
		} else {
			setTimeout(() => {
				uni.showToast({
					icon: 'none', //success、loading、none
					title: titles,
					duration: 2000
				})
			}, 500)
		}
	},
	showModal: function(title, content, status, id) {
		let titles = title || '提示';
		let contents = content;
		uni.showModal({
			title: titles,
			content: contents,
			editable: true,
			success: function(res) {
				if (res.confirm) {
					let sendData = {
						member_id: uni.getStorageSync("member_id"),
						status: status,
						desc: res.content,
						id: id,
					}
					publicFunction.callback(sendData)
				}
			}
		});
	},

	navigate: function(path, params) {
		let paramsData = params || '';
		let urls = path + '?' + paramsData;
		uni.navigateTo({
			url: urls,
			animationType: 'pop-in', // 窗口动画,只在APP有效
			animationDuration: 300,
		})
	},
	
	uploadFileimg: function(callback, that) {
		let thats = this;
		uni.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album'], //从相册选择
			success: (res) => {
				let tempFilePaths = res.tempFilePaths[0];
				uni.uploadFile({
					url: that.apifun.uploadimage,
					filePath: tempFilePaths,
					name: 'file', // 此处根据服务端提供字段进行更改
					success(res) {
						let datas = JSON.parse(res.data);
						callback(datas); // ->此处根据服务端提供字段进行更改
					}
				});
			}
		});
	},

	unirequest: function(url, met, datas, sfun, showloading) {
		var headers = {
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
			"token": uni.getStorageSync('token'),
		}
		// if (met.toUpperCase() == 'POST') {}
		// if (!showloading) {
		// 	uni.showLoading({
		// 		title: '请稍后...'
		// 	});
		// }
		uni.request({
			url: url,
			method: met,
			data: datas,
			header: headers,
			dataType: 'json',
			responseType: 'text',
			success: function(res) {
				let datas = res.data;
				if (datas.code === 401) {
					publicFunction.toast('您尚未登录；前去登录！');
					setTimeout(() => {
						publicFunction.navigate('/pages/login/login');
					}, 2000)
				} else {
					sfun(datas)
				}
				uni.hideLoading();
			},
			fail: function(err) {
				// console.log(err)
				if (!showloading) {
					uni.hideLoading();
				}
				uni.showModal({
					title: '提示',
					content: '网络出了点小差，请重试',
					showCancel: false
				})
			},
			complete: function() {
				if (!showloading) {
					uni.hideLoading();
				}
			}
		})
	}
}

export default publicFunction;
